package validateServlet;

import java.util.*;
import java.sql.*;
import javax.sql.*;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class DataStore extends HttpServlet {
	
	Connection conn;
	ResultSet result;
	PreparedStatement pstmt;
	String query;
	boolean flag=false;
	RequestDispatcher rd;
	
	public void makeConnection()
	{
		try
		{
			query="insert into clients(name,address,tel_number,interestedlocation) values(?,?,?,?)";
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			conn=DriverManager.getConnection("jdbc:odbc:clientDsn","scott","tiger");
			pstmt=conn.prepareStatement(query);
			
		}
		catch(Exception e)
		{
			System.out.println("Improper saving of data :"+e.getStackTrace());
		}
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
		{
			makeConnection();
			String name,address,phone,location;
			name=request.getParameter("nameText");
			address=request.getParameter("addressText");
			phone=request.getParameter("telText");
			location=request.getParameter("stateSelect");
			
			try{
				pstmt.setString(1, name);
				pstmt.setString(2,address);
				pstmt.setString(3,phone);
				pstmt.setString(4,location);
				pstmt.executeUpdate();
				flag=true;
				
			}
			catch(Exception e)
			{
				System.out.println("Data not store "+e.getStackTrace());
				rd=getServletContext().getRequestDispatcher("/Failure.html");
				rd.forward(request, response);
			}
			
			if (flag==true)
			{
				rd=getServletContext().getRequestDispatcher("/Succesful.html");
				rd.forward(request, response);
			}
			
		}
}